package com.tm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tm.annotation.WmLog;
import com.tm.entity.CategoryEntity;
import com.tm.enums.OperationTypeEnum;
import com.tm.service.CateGoryService;
import com.tm.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author huangp
 * @create 2023-04-04 13:46
 */
@RestController
@RequestMapping("category")
@Api("分类管理")
public class CateGoryController {

    @Autowired
    private CateGoryService cateGoryService;

    @GetMapping("page")
    @ApiOperation("查询分类信息")
    @WmLog(operationModule = "查询",operationType = OperationTypeEnum.QUERY,description = "这是查询模块")
    public Result page(@RequestParam(required = false,defaultValue = "1") Integer page ,
                       @RequestParam(required = false,defaultValue = "10") Integer pageSize ){
        Page<CategoryEntity> list = cateGoryService.pageList(page,pageSize);
        return Result.success(list);
    }



    @PostMapping
    @ApiOperation("新增分类信息")
    @WmLog(operationModule = "新增",operationType = OperationTypeEnum.INSERT,description = "这是新增模块")
    public Result addCategory(@RequestBody CategoryEntity categoryEntity){
        Boolean flag = cateGoryService.addCategory(categoryEntity);
        return Result.success(flag);
    }

    @GetMapping("list")
    public Result getCategoryList(@RequestParam("type") Integer type){
        if (type == 2){
            QueryWrapper<CategoryEntity> queryWrapper = new QueryWrapper<CategoryEntity>();
            queryWrapper.eq("type",2);
            List<CategoryEntity> categoryEntity = cateGoryService.list(queryWrapper);
            return Result.success(categoryEntity);
        }
        QueryWrapper<CategoryEntity> queryWrapper = new QueryWrapper<CategoryEntity>();
        queryWrapper.eq("type",1);
        List<CategoryEntity> categoryEntity = cateGoryService.list(queryWrapper);
        return Result.success(categoryEntity);

    }

    @DeleteMapping
    @ApiOperation("批量删除分类信息")
    @WmLog(operationModule = "批量删除",operationType = OperationTypeEnum.DELETE,description = "这是批量删除模块")
    public Result deleCategory(Long ids){
        Boolean flag = cateGoryService.removeById(ids);
        return Result.success(flag);
    }

    @PutMapping
    @ApiOperation("修改分类信息")
    @WmLog(operationModule = "修改",operationType = OperationTypeEnum.UPDATE,description = "这是修改模块")
    public Result update(@RequestBody CategoryEntity categoryEntity){
        UpdateWrapper<CategoryEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",categoryEntity.getId());
        return Result.success(cateGoryService.update(categoryEntity,updateWrapper));
    }

}
